
package trim.differentmaterials.core;

import java.util.*;
import trim.common.*;
import trim.differentmaterials.generator.columns.*;
import trim.differentmaterials.generator.core.*;
import trim.common.inputdata.*;

/**
 *
 * @author Podorov Alexander
 */

public abstract class RestTrimmer
{
    protected final int materialCount;
    protected final int formatCount;

    protected final MaterialStock[] materialStock;
    protected final FormatOrder[] formatOrder;

    protected final int[] materialRests;
    protected final int[] formatRests;
    protected final int[] rightPart;

    protected final List<Column> patterns;
    protected final Generator generator;

    public RestTrimmer(TrimCore core, Generator generator)
    {
        materialCount = core.getMaterialCount();
        formatCount = core.getFormatCount();

        materialStock = core.getMaterialStock();
        formatOrder = core.getFormatOrder();

        materialRests = core.getMaterialRests();
        formatRests = core.getFormatRests();
        rightPart = core.getRightPart();

        patterns = core.getPatterns();
        this.generator = generator;
    }

    protected int materialToIndex(Material material)
    {
        int index = - 1;
        for (int i = 0; i < materialCount; i++)
        {
            if (materialStock[i].getMaterial() == material)
            {
                index = i;
            }
        }
        return index;
    }

    protected void setMaterialRests()
    {
        for (int i = 0; i < materialCount; i++)
        {
            materialRests[i] = rightPart[i + formatCount];
        }
        for (int i = 0; i < patterns.size(); i++)
        {
            RealPattern pattern = (RealPattern)patterns.get(i);
            materialRests[materialToIndex(pattern.getMaterial())] -= pattern.getCount();
        }
    }

    protected void setFormatRests()
    {
        for (int i = 0; i < formatCount; i++)
        {
            formatRests[i] = rightPart[i];
            for (int j = 0; j < patterns.size(); j++)
            {
                RealPattern pattern = (RealPattern)patterns.get(j);

                int[] items = pattern.getItems();
                formatRests[i] -= pattern.getCount() * items[i];
            }
        }
    }

    protected boolean isMaterialRests()
    {
        for (int i = 0; i < materialCount; i++)
        {
            if (materialRests[i] > 0)
            {
                return true;
            }
        }
        return false;
    }

    protected boolean isFormatRests()
    {
        for (int i = 0; i < formatCount; i++)
        {
            if (formatRests[i] > 0)
            {
                return true;
            }
        }
        return false;
    }

    public abstract void start() throws NoSolutionsException;
}
